/******************************************************************************
 * @File name   :      CardDefinitionDaoImpl.java
 *
 * @Author      :      JIZHANGS
 *
 * @Date        :      Aug 11, 2015
 *
 * @Copyright Notice: 
 * Copyright (c) 2015 Capgemini, Inc. All  Rights Reserved.
 * This software is published under the terms of the Capgemini Software
 * License version 1.0, a copy of which has been included with this
 * distribution in the LICENSE.txt file.
 * 
 * 
 * ----------------------------------------------------------------------------
 * Date                   Who         Version        Comments
 * Aug 11, 2015 6:02:26 PM        JIZHANGS     1.0            Initial Version
 *****************************************************************************/
package com.cap.bts.crmdp.dealer.card.dao.impl;

import java.util.List;

import javax.inject.Inject;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import com.cap.bts.crmdp.dealer.card.dao.ICardCreateDao;
import com.cap.bts.crmdp.dealer.card.domain.CardDefinition;
import com.cap.bts.crmdp.dealer.card.domain.CardGeneration;
import com.cap.bts.framework.common.dao.impl.BaseDaoImpl;
import com.cap.bts.framework.common.exception.AppException;

/**
 *
 */
@Repository("cardCreateDao")
public class CardCreateDaoImpl extends BaseDaoImpl<CardGeneration, Long>implements ICardCreateDao {

    /**
     * @Date        :      Aug 11, 2015
     * @param clazz
     */
    @Inject
    public CardCreateDaoImpl(@Value("com.cap.bts.crmdp.dealer.card.domain.CardGeneration")Class<CardGeneration> clazz) {
        super(clazz);
    }

    /**
     * {@inheritDoc} 
     * overridden:
     * @Date        :      Aug 19, 2015
     * @see com.cap.bts.crmdp.dealer.card.dao.ICardCreateDao#getNextCardNumFrom(com.cap.bts.crmdp.dealer.card.domain.CardDefinition)
    **/
    @Override
    public String getNextCardNumFrom(CardDefinition cd) throws AppException {
        String sql="SELECT func_cardnum_plus_1('"+cd.getCardNum()+"','"+cd.getPerfix()+"',"+cd.getLengthOfDigital()+","+cd.getCheckBitEnabeld()+")";
        return (String) super.getEntityManager().createNativeQuery(sql).getSingleResult();
    }

    /**
     * {@inheritDoc} 
     * overridden:
     * @Date        :      Aug 19, 2015
     * @see com.cap.bts.crmdp.dealer.card.dao.ICardCreateDao#findAllCardGenerationList()
    **/
    @Override
    public List<CardGeneration> findAllCardGenerationList() throws AppException {
        return super.getListByQuery("from CardGeneration a where a.cardDefinition.status=1");
    }

    /**
     * {@inheritDoc} 
     * overridden:
     * @Date        :      Aug 19, 2015
     * @see com.cap.bts.crmdp.dealer.card.dao.ICardCreateDao#getNextCardNumTo(int, int)
    **/
    @Override
    public String getNextCardNumTo(CardDefinition cd, int quantity) throws AppException {
        String sql="SELECT func_cardnum_plus_n('"+cd.getCardNum()+"','"+cd.getPerfix()+"',"+cd.getLengthOfDigital()+","+cd.getCheckBitEnabeld()+","+quantity+")";
        return (String) super.getEntityManager().createNativeQuery(sql).getSingleResult();
    }

    /**
     * {@inheritDoc} 
     * overridden:
     * @Date        :      Aug 22, 2015
     * @see com.cap.bts.crmdp.dealer.card.dao.ICardCreateDao#generateCard(java.lang.String)
    **/
    @Override
    public Boolean generateCard(String generateInfo) throws AppException {
        super.getEntityManager().createNativeQuery("{ call SSP_CARD_GENERATION_ADD('"+generateInfo+"')}").executeUpdate();
        return true;
    }

    /**
     * {@inheritDoc} 
     * overridden:
     * @Date        :      Aug 25, 2015
     * @see com.cap.bts.crmdp.dealer.card.dao.ICardCreateDao#getCheckBit(com.cap.bts.crmdp.dealer.card.domain.CardDefinition)
    **/
    @Override
    public String getCheckBit(CardDefinition dto) throws AppException {
        String sql="SELECT func_checkbit('"+dto.getCardNum()+"','"+dto.getPerfix()+"',"+dto.getLengthOfDigital()+","+dto.getCheckBitEnabeld()+")";
        return (String) super.getEntityManager().createNativeQuery(sql).getSingleResult();
    }

    /**
     * {@inheritDoc} 
     * overridden:
     * @Date        :      Aug 25, 2015
     * @see com.cap.bts.crmdp.dealer.card.dao.ICardCreateDao#getActiveCode(com.cap.bts.crmdp.dealer.card.domain.CardDefinition)
    **/
    @Override
    public String getActiveCode(CardDefinition dto) throws AppException {
        String sql="SELECT func_activecode("+dto.getActivationCodeType()+","+dto.getActivationCodeLength()+")";
        return (String) super.getEntityManager().createNativeQuery(sql).getSingleResult();
    }
  
}
